{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python as a calculator\n",
    "\n",
    "\n",
    "Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:\n",
    "\n",
    "Exponentiation: **. This operator raises the number to its left to the power of the number to its right. For example 4**2 will give 16.\n",
    "Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4.\n",
    "The code in the script gives some examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "0\n",
      "15\n",
      "5.0\n",
      "4\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "# Addition, subtraction\n",
    "print(5 + 5)\n",
    "print(5 - 5)\n",
    "\n",
    "# Multiplication, division, modulo, and exponentiation\n",
    "print(3 * 5)\n",
    "print(10 / 2)\n",
    "print(18 % 7)\n",
    "print(4 ** 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variable Assignment\n",
    "In Python, a variable allows you to refer to a value with a name. To create a variable use =, like this example:\n",
    "\n",
    "x = 5\n",
    "You can now use the name of this variable, x, instead of the actual value, 5.\n",
    "\n",
    "Remember, = in Python means assignment, it doesn't test equality!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Type conversion\n",
    "\n",
    "Using the + operator to paste together two strings can be very useful in building custom messages.\n",
    "\n",
    "Suppose, for example, that you've calculated the return of your investment and want to summarize the results in a string. Assuming the integer savings and float result are defined, you can try something like this:\n",
    "\n",
    "print(\"I started with $\" + savings + \" and now have $\" + result + \". Awesome!\")\n",
    "This will not work, though, as you cannot simply sum strings and integers/floats.\n",
    "\n",
    "To fix the error, you'll need to explicitly convert the types of your variables. More specifically, you'll need str(), to convert a value into a string. str(savings), for example, will convert the integer savings to a string.\n",
    "\n",
    "Similar functions such as int(), float() and bool() will help you convert Python values into any type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I started with $100 and now have $194.87171000000012. Awesome!\n"
     ]
    }
   ],
   "source": [
    "# Definition of savings and result\n",
    "savings = 100\n",
    "result = 100 * 1.10 ** 7\n",
    "\n",
    "# Fix the printout\n",
    "print(\"I started with $\" + str(savings) + \" and now have $\" + str(result) + \". Awesome!\")\n",
    "\n",
    "# Definition of pi_string\n",
    "pi_string = \"3.1415926\"\n",
    "\n",
    "# Convert pi_string into float: pi_float\n",
    "# write your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## import package\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "math.sqrt(16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.324555320336759"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from math import sqrt\n",
    "sqrt(40)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Commonly used packages including: Numpy, Pandas, Matplotlib, Scipy**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy is short for \"Numerical Python\".\n",
    "# Numpy working with arrays\n",
    "# Matplotlib is for plot and visualization\n",
    "# Pandas wors with tables\n",
    "# SciPy provides algorithms for optimization, integration, interpolation, \n",
    "# eigenvalue problems, algebraic equations, differential equations, statistics and many other classes of problems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A function is a block of code which only runs when it is called. \n",
    "You can pass data, known as parameters, into a function.\n",
    "A function can return data as a result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "\n",
    "def f(a, b):\n",
    "    I = sqrt(a**2 + b**2)\n",
    "    return I\n",
    "\n",
    "result = f(3, 4)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# for loop example\n",
    "for i in list(range(5)):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "Loop ends.\n"
     ]
    }
   ],
   "source": [
    "# while loop example\n",
    "count = 0\n",
    "while count < 5:\n",
    "    print(count)\n",
    "    count = count+1\n",
    "    \n",
    "print('Loop ends.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is greater than b\n"
     ]
    }
   ],
   "source": [
    "# if...else...loop\n",
    "a = 200\n",
    "b = 33\n",
    "if b > a:\n",
    "    print(\"b is greater than a\")\n",
    "elif a == b:\n",
    "    print(\"a and b are equal\")\n",
    "else:\n",
    "    print(\"a is greater than b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "!!!  **Indentation is very important in python**\n",
    "\n",
    "Indentation refers to the spaces at the beginning of a code line.\n",
    "\n",
    "Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.\n",
    "\n",
    "Python uses indentation to indicate a block of code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Exercise\n",
    "\n",
    "1. use numpy to build an equal spaced sequence from zero to ten(google np.linspace)\n",
    "2. use matplotlib to plot a function f(x) = x^2 -2*x+3\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
